/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package linkeddata.arida.ufc.br.util;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;
import org.semanticweb.HermiT.Reasoner;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.OWLException;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyIRIMapper;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.reasoner.OWLReasoner;
import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;
import org.semanticweb.owlapi.util.DefaultPrefixManager;

/**
 *
 * @author jeovane
 */
public class OWLUtils {

    private OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    private OWLOntology ontology;
    private OWLOntologyIRIMapper ontologyIRIMapper;
    private OWLReasonerFactory factory;
    private OWLReasoner reasoner;
    private DefaultPrefixManager pm;
    private TripleStore ts;
    private String query;
    protected String queryUnion;
    protected String currentClass;
    private String prefix;
    private String nameDataProperty;
    private Set<String> setDomain = new LinkedHashSet<String>();
    private ArrayList<String> arrayRange = new ArrayList<String>();
    private ArrayList<String> arrayIsRange = new ArrayList<String>();
    private Set<String> setSelect = new LinkedHashSet<String>();
    private Set<String> setWhere = new LinkedHashSet<String>();
    protected Set<String> setWhereUnion = new LinkedHashSet<String>();
    private Set<String> setOptional = new LinkedHashSet<String>();
    private ArrayList<String> arrayKeysMap = new ArrayList<String>();
    protected ArrayList<String> arrayCurrentClass;
    private ArrayList<String> arrayObjectDomain = new ArrayList<String>();
    protected Set<String> arraySelectUnion = new LinkedHashSet<String>();
    //private File file;
    private File file = new File("/home/jeovane/Dropbox/Public/Onto_DD.owl");
    //protected File file = new File("E:\\Dropbox\\Public\\Onto_DD.owl");
    //protected File file = new File("/home/jeovane/Dropbox/Public/Diseases_Drugs.owl");
    //protected File file = new File("/home/jeovane/Documentos/Ontologias/pizza.owl");
    //protected File file = new File("/home/jeovane/Documentos/Ontologias/VCDTici.owl");
    //protected File file = new File("E:\\Dropbox\\Public\\VCDTici.owl");

    public OWLUtils() {
    }

    public OWLUtils(String uri) {
        file = new File(uri);
    }

    private OWLOntology loadOntology() throws OWLOntologyCreationException {
        this.ontology = manager.loadOntologyFromOntologyDocument(getFile());
        return this.ontology;
    }

    private DefaultPrefixManager prefixManager() throws OWLOntologyCreationException {
        this.pm = new DefaultPrefixManager(ontology.getOntologyID().getOntologyIRI() + "#");
        return this.pm;
    }

    private void baseLoaded() throws OWLOntologyCreationException {
        ontology = loadOntology();
        pm = prefixManager();
        factory = new Reasoner.ReasonerFactory();
        reasoner = factory.createReasoner(ontology);
    }

    public void ontologyLoaded() {
        if (getOntology() == null) {
            try {
                baseLoaded();
            } catch (OWLException erro) {
                System.out.println("Erro: " + erro);
            }
        }
    }

    public OWLOntology getOntology() {
        return ontology;
    }

    public DefaultPrefixManager getPm() {
        return pm;
    }
    
    public OWLReasoner getReasoner() {
        return reasoner;
    }

    public void setTs(TripleStore ts) {
        this.ts = ts;
    }

    public TripleStore getTs() {
        return ts;
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public Set<String> getSetDomain() {
        return setDomain;
    }

    public void setSetDomain(String setDomain) {
        this.setDomain.add(setDomain);
    }

    public ArrayList<String> getArrayRange() {
        return arrayRange;
    }

    public void setArrayRange(String arrayRange) {
        this.arrayRange.add(arrayRange);
    }

    public ArrayList<String> getArrayIsRange() {
        return arrayIsRange;
    }

    public void setArrayIsRange(String arrayIsRange) {
        this.arrayIsRange.add(arrayIsRange);
    }

    public Set<String> getSetSelect() {
        return setSelect;
    }

    public void setSetSelect(String setSelect) {
        this.setSelect.add(setSelect);
    }

    public Set<String> getSetWhere() {
        return setWhere;
    }

    public void setSetWhere(String setWhere) {
        this.setWhere.add(setWhere);
    }

    public Set<String> getSetOptional() {
        return setOptional;
    }

    public void setSetOptional(String setOptional) {
        this.setOptional.add(setOptional);
    }

    public ArrayList<String> getArrayKeysMap() {
        return arrayKeysMap;
    }

    public void setArrayKeysMap(String arrayKeysMap) {
        this.arrayKeysMap.add(arrayKeysMap);
    }

    public ArrayList<String> getArrayObjectDomain() {
        return arrayObjectDomain;
    }

    public void setArrayObjectDomain(String arrayObjectDomain) {
        this.arrayObjectDomain.add(arrayObjectDomain);
    }

    public File getFile() {
        return file;
    }
}
